package com.night.xjh.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.github.pagehelper.PageInfo;
import com.night.common.entity.ResultVo;
import com.night.common.entity.xjh.Visit;
import com.night.xjh.vo.VisitVo;
import com.night.xjh.service.VisitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;

/**
 * @ClassName: VisitController
 * @Description: //TODO
 * @author: xjh
 * @date: 2022/10/24-9:12
 * @version: v1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/xjh/visit")
public class VisitController {
    @Autowired
    private VisitService visitService;
    public ResultVo<?> withBlockHandler(BlockException ex){
        ex.printStackTrace();
        log.info("限流：异常{}",ex.getMessage());
        return ResultVo.ERROR;
    }
    public ResultVo<?> withFallback(){
        return ResultVo.FAIL;
    }
    @RequestMapping("/add")
    @SentinelResource(value = "add",blockHandler = "withBlockHandler",fallback = "withFallback")
    public ResultVo<?> add(@RequestBody VisitVo visitVo){
        try {
            visitService.add(dataFormat(visitVo));
        }catch (Exception e){
            return ResultVo.ERROR;
        }
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/delete")
    @SentinelResource(value = "delete",blockHandler = "withBlockHandler")
    public ResultVo<?> delete(@RequestBody VisitVo visitVo){
        try {
            visitService.delete(visitVo.getId());
        }catch (Exception e){
            return ResultVo.ERROR;
        }
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/update")
    @SentinelResource(value = "update",blockHandler = "withBlockHandler")
    public ResultVo<?> update(@RequestBody VisitVo visitVo){
        try {
            visitService.update(dataFormat(visitVo));
        }catch (Exception e){
            return ResultVo.ERROR;
        }
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/getList")
    public ResultVo<?> getList(@RequestBody VisitVo visitVo){
        if(visitVo.getVisitDate()!=null&&!visitVo.getVisitDate().equals("")){
            return new ResultVo<>(visitService.getByCondition(dataFormat(visitVo),visitVo.getPageNum(),visitVo.getPageSize()));
        }else if((visitVo.getStartDate()!=null&&!visitVo.getStartDate().equals(""))|| (visitVo.getEndDate()!=null&&!visitVo.getEndDate().equals(""))){
            return new ResultVo<>(visitService.getByDate(visitVo.getStartDate(),
                    visitVo.getEndDate(),visitVo.getPageNum(),visitVo.getPageSize()));
        }else{
            return new ResultVo<>(visitService.getAll(visitVo.getPageNum(),visitVo.getPageSize()));
        }
    }
    //返回Visit实体
    @RequestMapping("/getById")
    @SentinelResource(value = "getById",blockHandler = "withBlockHandler")
    public ResultVo<?> getById(@RequestBody VisitVo visitVo){
        try {
            return new ResultVo<>(visitService.getById(visitVo.getId()));
        }catch (Exception e){
            return ResultVo.ERROR;
        }
    }
    //前端传来的VisitVo转化Visit实体
    private Visit dataFormat(VisitVo visitVo){
        Visit visit=new Visit();
        visit.setId(visitVo.getId());
        visit.setName(visitVo.getName());
        visit.setPhone(visitVo.getPhone());
        visit.setVisitDate(visitVo.getVisitDate());
        return visit;
    }
}
